package _18_剑指OfferII;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _081_剑指OfferII允许重复选择元素的组合 {

    public static void main(String[] args) {

        _081_剑指OfferII允许重复选择元素的组合 v = new _081_剑指OfferII允许重复选择元素的组合();

        int[] ins = {2, 3, 6, 7};
        int target = 7;
        System.out.println(v.combinationSum(ins, target));

    }

    private List<List<Integer>> results = new ArrayList<>();

    public List<List<Integer>> combinationSum(int[] candidates, int target) {

        // 对数组元素排序
        Arrays.sort(candidates);

        dfs(0, candidates, target, new ArrayList<>());
        return results;
    }

    private void dfs(int curIndex, int[] candidates, int target, List<Integer> list) {
        if (target == 0) {
            results.add(new ArrayList<>(list));
            return;
        }
        for (int i = curIndex; i < candidates.length; i++) {
            int cur = candidates[i];
            if (target < cur) break;
            list.add(cur);
            dfs(i, candidates, target - cur, list);
            list.remove(list.size() - 1);
        }
    }

}
